var sellerCode="";
$(document).ready(function() {
	$("#sellers").autocomplete({
		source : function(request, response) {
			$.ajax({
				url : contextPath + '/iccard/report/getSellers',
				dataType : "json",
				data : {
					maxRows : 10,
					keyword : request.term
				},
				success : function(data) {
					sellerCode = "";
					response($.map(data, function(item) {
						return {
							label : item.value + ',' + item.label,
							value : item.label
						};
					}));
				}
			});
		},
		minLength : 2,
		select : function(event, ui) {
			sellerCode = ui.item.label;
		},
		open : function() {
			$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
		},
		close : function() {
			$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
		}
	});
	doSearch();
});

function openSelector() {
	Boxy.seller($("#sellerIds").val(), contextPath, function(val) {
		var names = "";
		var values = "";
		if (val != "") {
			var choosed = val.split(",");
			for ( var i = 0; i < choosed.length; i++) {
				var t = choosed[i].split("|");
				names += t[1] + ",";
				values += t[0] + ",";
			}
		}
		$("#sellers").val(names);
		$("#sellerIds").val(values);
	}, {
		title : "商家选择器"
	});
	return false;
}

function doSearch() {
	$("#data_list").datagrid({
		onLoadError : function(data) {
			uppAjaxError(data);
		},
		queryParams : {
			sellerCode : sellerCode
		},
		columns : [ [ {
			field : 'merchantName',
			title : '商家',
			width : 100,
			align : 'center'
		}, {
			field : 'merchantCode',
			title : '商家编号',
			width : 100,
			align : 'center'
		}, {
			field : 'billingStatus',
			title : '状态',
			width : 100,
			align : 'center',
			formatter : function(value, row, index) {
				if (value == '0') {
					return '暂不结算';
				} else if (value == '1') {
					return '正常';
				} else {
					return '正常';
				}
			}
		}, {
			field : 'billingStatus2',
			title : '操作',
			width : 100,
			align : 'center',
			formatter : function(value, row, index) {
				var link = "暂不结算";
				var ss = value.split(",");
				var ss0 = ss[0];
				var ss1 = ss[1];
				var aa ;
				if (ss1 == '0') {
					link = '开启结算';
				} else if (ss1 == '1') {
					link ='暂不结算';
				} else {
					link ='暂不结算333';
				}
				aa = '<a id="openOrclose" href="#" class="easyui-linkbutton" onclick=openOrclose('+ss0+','+ss1+');>'+link+'</a>';
				return aa;
			}
		} ] ],
		autoRowHeight : true,
		fitColumns : true,
		pagination : true,
		rownumbers : true,
		singleSelect : true
	});
}

function openOrclose(str1,str2){
	var merchantId = str1;
	var status = str2;
//	alert("merchantId="+merchantId+",status:"+status);
	url = contextPath + "iccard/financialmanage/changeStatus?merchantId="+merchantId+"&status="+status+"&rid=" + Math.random();
//	alert("usl="+url);
	$.ajax({
		type : 'POST',
		url : url,
		dataType : 'json',
		success : function(root) {
//			alert('111111111111');
//			if(status == 1){
//				$('#openOrclose').text('开启结算');
//			}
//			else if(status == 0){
//				$('#openOrclose').text('暂不结算');
//			}
			doSearch();
			top.uppShow(root.msgDesc);
			// 隐藏保存按钮
//			$("#btn_div").hide();
//			window.top.reload_combocard_list.call();
//			$("#savab").attr('disabled', '');
		},
		error : function(data) {
			uppAjaxError(data);
		}
	});
}
